// pages/title/title_list/title_list.js
import {
  formatTime
} from '../../../../utils/date_formatter'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userType:null,
    keyword: "",
    Page: {
      pageSize: 10,
      pageNum: 0,
      totalCount: 0,
    },
    titleList: [],
    loading: false
  },
  to_add_title() {
    wx.navigateTo({
      url: '/subIndex/pages_first/title/add_title/add_title',
    })
  },
  // 获取公告列表
  async getTitleList() {
    wx.showLoading({
      title: '数据加载中',
    })
    this.setData({
      loading: true
    })
    try {
      this.data.Page.pageNum += 1
      const json = await getApp().$axios("/getAllNotice", "POST", this.data.Page)
      wx.hideLoading()
      if (json.status !== 200) {
        wx.showToast({
          title: json.data,
          icon: 'error'
        })
        return
      }
      this.setData({
        titleList: [...this.data.titleList, ...json.data],
        Page: json.page,
        loading: false
      })
    } catch (error) {
      wx.showToast({
        title: error.msg,
        icon: 'none'
      })
    }
  },
  // 去公告详细
  to_title_detail(e) {
    const id = e.currentTarget.dataset.tid;
    const type = e.currentTarget.dataset.type;
    if (type==1) {
      wx.showToast({
        title: "公告没有详细！",
        icon:'error'
      })
      return
    }
    wx.navigateTo({
      url: '/subIndex/pages_first/title/detail_title/detail_title?id=' + id,
    })
  },
  // onScrollToLower下拉触底
  onScrollToLower() {
    console.log("到底");
    if (!this.data.loading && this.data.Page.pageNum * this.data.Page.pageSize <= this.data.Page.totalCount && (this.data.keyword == null || this.data.keyword == "")) {
      console.log("到底部了");
      this.getTitleList()
    }
  },
  // 删除公告
  async deleteNotice(e) {
    const id = e.currentTarget.dataset.id
    try {
      const res = await getApp().$axios("/deleteNotice", "POST", {
        id
      })
      if (res.status !== 200) {
        wx.showToast({
          title: res.data,
          icon: 'none'
        })
        return
      }
      this.setData({
        Page: {
          pageSize: 10,
          pageNum: 0,
          totalCount: 0,
        },
        titleList:[]
      })
      this.getTitleList()
    } catch (error) {
      wx.showToast({
        title: error.msg,
        icon: 'error'
      })
    }
  },
  // 收集搜索关键字
  keywordChange(e){
    let keyword = e.detail;
    this.setData({
      keyword: keyword
    })
  },
  //清空关键字
  onCancel(){
    this.setData({
      keyword:null,
      Page: {
        pageSize: 10,
        pageNum: 0,
        totalCount: 0,
      },
      titleList:[]
    })
    this.getTitleList()
  },
  // 搜索
  async onSearch(){
    try {
      const json = await getApp().$axios("/getNoticeByTitle", "POST", {
        title: this.data.keyword
      })
      if (json.status !== 200) {
        wx.showToast({
          title: json.data,
          icon: 'error'
        })
        return
      }
      this.setData({
        titleList: json.data
      })
    } catch (error) {
      wx.showToast({
        title: error.msg,
        icon: 'error'
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // this.getTitleList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.setData({
      userType:getApp().globalData.userInfo.identity_ID
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      Page: {
        pageSize: 10,
        pageNum: 0,
        totalCount: 0,
      },
      titleList:[],
      keyword:null
    })
    this.getTitleList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})